%% in this file, we try to segment the car from the image.

pathOri = 'image_folder1/image_';
finalPath = 'segmentCar/image_';

mask = imread('mask/mask.jpg', 'jpg');

grayMask = zeros(240,352);

grayMask = rgb2gray(mask);

threshold = 20;

for i = 2 : 2
    
    imPath = sprintf('%s%04d.jpg', pathOri,i)
    image = imread(imPath);
    
    grayImage = zeros(240,352);
    grayImage = rgb2gray(image);
    
    
    h = fspecial('gaussian', [3,3], 1.0);
    grayImage = imfilter(grayImage, h, 'replicate');
    
    grayMask = imfilter(grayMask, h, 'replicate');
    
  
    result = zeros(240, 352);
    result = imsubtract(grayMask, grayImage);
    
    
    result = result > threshold;
    
    
    result = double(result);
    result = imfill(result);
    se = strel('line',11,90);
    result = imdilate(result,se);
    result = uint8(result);
    
    grayImage = uint8(grayImage);
    grayImage = grayImage.*result;
    
    figure();
    imshow(grayImage);
    
    %rgbImage = repmat(grayImage,[1 1 3]);
    %rgbImage = repmat(double(grayImage)./255,[1 1 3]);
    rgbImage = cat(3,grayImage,grayImage,grayImage);
    figure();
    imshow(rgbImage);
    
    %rgbImage = repmat(grayImage,[1 1 3]);
    
    %figure();
    %imshow(result);
    %imageSum = zeros(240,352,3);
    %imageSum(:,:,1) = result.*image(:,:,1);
    %imageSum(:,:,2) = result.*image(:,:,2);
    %imageSum(:,:,3) = result.*image(:,:,3);
    
    %figure();
    %imshow(imageSum);
    
    %result =  result > threshold;
    %figure();
    %imshow(result2);
    
    %imwrite(result, 'result.jpg', 'jpg');
    
    
end